package com.castle.fortress.admin.member.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.member.dto.MemberSetingDto;
import com.castle.fortress.admin.member.entity.MemberSetingEntity;
import com.castle.fortress.admin.member.service.MemberSetingService;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员设置表 api 控制器
 *
 * @author Mgg
 * @since 2021-11-26
 */
@Api(tags="会员设置表api管理控制器")
@RestController
@RequestMapping("/api/member/memberSeting")
public class ApiMemberSetingController {
    @Autowired
    private MemberSetingService memberSetingService;


    /**
     * 会员设置表的分页展示
     * @param memberSetingDto 会员设置表实体类
     * @param currentPage 当前页
     * @param size  每页记录数
     * @return
     */
    @ApiOperation("会员设置表分页展示")
    @GetMapping("/page")
    public RespBody<IPage<MemberSetingDto>> pageMemberSeting(MemberSetingDto memberSetingDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
        Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<MemberSetingDto> page = new Page(pageIndex, pageSize);

        IPage<MemberSetingDto> pages = memberSetingService.pageMemberSeting(page, memberSetingDto);
        return RespBody.data(pages);
    }

    /**
     * 会员设置表保存
     * @param memberSetingDto 会员设置表实体类
     * @return
     */
    @ApiOperation("会员设置表保存")
    @PostMapping("/save")
    public RespBody<String> saveMemberSeting(@RequestBody MemberSetingDto memberSetingDto){
        if(memberSetingDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            MemberSetingEntity memberSetingEntity = ConvertUtil.transformObj(memberSetingDto,MemberSetingEntity.class);
        if(memberSetingService.save(memberSetingEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员设置表编辑
     * @param memberSetingDto 会员设置表实体类
     * @return
     */
    @ApiOperation("会员设置表编辑")
    @PostMapping("/edit")
    public RespBody<String> updateMemberSeting(@RequestBody MemberSetingDto memberSetingDto){
        if(memberSetingDto == null || memberSetingDto.getId() == null || memberSetingDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            MemberSetingEntity memberSetingEntity = ConvertUtil.transformObj(memberSetingDto,MemberSetingEntity.class);
        if(memberSetingService.updateById(memberSetingEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员设置表删除
     * @param ids 会员设置表id集合
     * @return
     */
    @ApiOperation("会员设置表删除")
    @PostMapping("/delete")
    public RespBody<String> deleteMemberSeting(@RequestBody List<Long> ids){
        if(ids == null || ids.isEmpty()){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(memberSetingService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员设置表详情
     * @param id 会员设置表id
     * @return
     */
    @ApiOperation("会员设置表详情")
    @GetMapping("/info")
    public RespBody<MemberSetingDto> infoMemberSeting(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            MemberSetingEntity memberSetingEntity = memberSetingService.getById(id);
            MemberSetingDto memberSetingDto = ConvertUtil.transformObj(memberSetingEntity,MemberSetingDto.class);
        return RespBody.data(memberSetingDto);
    }


}
